package io.renren.modules.yunxin;

import com.alibaba.fastjson.JSONObject;

import io.renren.common.utils.StringUtils;
import org.apache.http.Consts;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public class YunxinLiveUtils {

    static String appKey = "94kid09c9ig9k1loimjg012345123456";
    static String appSecret = "123456789012";

    private static Logger logger = LoggerFactory.getLogger(YunxinLiveUtils.class);

    public static Live createLive(String name) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = "https://vcloud.163.com/app/channel/create";
        HttpPost httpPost = new HttpPost(url);

        String nonce = StringUtils.getUUID();
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = YunxinUtils.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码

        // 设置请求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/json;charset=utf-8");

        // 设置请求的参数
        StringEntity params = new StringEntity("{\"name\":\"" + name + "\", \"type\":0}", Consts.UTF_8);
        CloseableHttpResponse response = httpClient.execute(httpPost);

        String ret = EntityUtils.toString(response.getEntity(), "utf-8");

        JSONObject jsonRet = new JSONObject();
        jsonRet = JSONObject.parseObject(ret);

        if (jsonRet.isEmpty() || jsonRet.getInteger("code") != 200) {
            logger.info("注册返回 :" + ret);
            throw new Exception("创建频道失败:" + jsonRet.getString("msg"));
        }
        return JSONObject.parseObject(jsonRet.getString("ret"), Live.class);
    }


    public static void deleteLive(String cid) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String url = "https://vcloud.163.com/app/channel/delete";
        HttpPost httpPost = new HttpPost(url);
        String nonce = StringUtils.getUUID();
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = YunxinUtils.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码
        // 设置请求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
        // 设置请求的参数
        StringEntity params = new StringEntity("{ \"cid\":\"" + cid + "\"}", Consts.UTF_8);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        String ret = EntityUtils.toString(response.getEntity(), "utf-8");
        JSONObject jsonRet = new JSONObject();
        jsonRet = JSONObject.parseObject(ret);
        if (jsonRet.isEmpty() || jsonRet.getInteger("code") != 200) {
            logger.error("删除返回 :" + ret);
            throw new Exception("删除频道失败:" + jsonRet.getString("msg"));
        }
    }
}
